﻿using System.Text.RegularExpressions;
using System.Windows.Forms;
using EdlinSoftware.RegexTester.Properties;

namespace EdlinSoftware.RegexTester.Controls
{
    public class MatchesTreeView : TreeView
    {
        #region Methods

        public void ShowMatches(MatchCollection matches)
        {
            Nodes.Clear();

            if (AreThereNoMatches(matches))
            { AddNodeForNoMatches(this); }
            else
            { AddNodesForMatches(this, matches); }
        }

        private static bool AreThereNoMatches(MatchCollection matches)
        {
            return matches == null || matches.Count == 0;
        }

        private static void AddNodeForNoMatches(TreeView treeview)
        {
            treeview.Nodes.Add(new TreeNode(Resources.NoMatches));
        }

        private static void AddNodesForMatches(TreeView treeview, MatchCollection matches)
        {
            int index = 1;
            foreach (Match match in matches)
            {
                TreeNode node = CreateNodeForCapture(match, "Match", index);

                TreeNode groupsNode = new TreeNode("Groups");
                node.Nodes.Add(groupsNode);
                AddNodesForGroups(groupsNode, match.Groups);

                TreeNode capturesNode = new TreeNode("Captures");
                node.Nodes.Add(capturesNode);
                AddNodesForCaptures(capturesNode, match.Captures);

                treeview.Nodes.Add(node);
                index++;
            }
        }

        private static TreeNode CreateNodeForCapture(Capture capture, string type, int index)
        {
            TreeNode node = new TreeNode
            {
                Text = string.Format("{0} {1}", type, index),
                Tag = capture
            };

            node.Nodes.Add(new TreeNode { Text = "Index: " + capture.Index, Tag = capture });
            node.Nodes.Add(new TreeNode { Text = "Length: " + capture.Length, Tag = capture });
            node.Nodes.Add(new TreeNode { Text = "Value: " + capture.Value, Tag = capture });

            return node;
        }

        private static void AddNodesForGroups(TreeNode groupsNode, GroupCollection groups)
        {
            int index = 1;
            foreach (Group group in groups)
            {
                TreeNode node = CreateNodeForCapture(group, "Group", index);

                TreeNode capturesNode = new TreeNode("Captures");
                node.Nodes.Add(capturesNode);
                AddNodesForCaptures(capturesNode, group.Captures);

                groupsNode.Nodes.Add(node);
                index++;
            }
        }

        private static void AddNodesForCaptures(TreeNode capturesNode, CaptureCollection captures)
        {
            int index = 1;
            foreach (Capture capture in captures)
            {
                TreeNode node = CreateNodeForCapture(capture, "Capture", index);

                capturesNode.Nodes.Add(node);
                index++;
            }
        }

        #endregion
    }
}
